import { createRouter, createWebHashHistory } from 'vue-router'
import store from '../store/index'
import { getRoleById } from '@/utils'
import { getToken } from '@/utils/token'
import { message } from 'ant-design-vue'

const routes = [
  {
    path: '/auth',
    name: 'Auth',
    component: () => import(/* webpackChunkName: "auth" */ '../views/auth/login.vue')
    ,
  },
  {
    path: '/',
    name: 'Layout',
    redirect: '/auth',
    component: () => import(/* webpackChunkName: "layout" */  '../layout/index.vue'),
    children: [
      {
        path: '/dashboard',
        name: 'Dashboard',
        component: () => import(/* webpackChunkName: "dashboard" */  '../views/dashboard/index'),
      },
      {
        path: 'book',
        name: 'Book',
        component: () => import(/* webpackChunkName: "book" */  '../views/book/index'),
      },
      {
        path: 'book/:id',
        name: 'Detail',
        component: () => import(/* webpackChunkName: "detail" */  '../views/book/BookDetail'),
      },
      {
        path: 'inventory',
        name: 'Inventory',
        component: () => import(/* webpackChunkName: "inventory" */  '../views/inventory/index'),
      },
      {
        path: 'user',
        name: 'User',
        component: () => import(/* webpackChunkName: "user" */  '../views/users/index'),
      },
      {
        path: 'log',
        name: 'Log',
        component: () => import(/* webpackChunkName: "log" */  '../views/log/index'),
      },
      {
        path: 'forget-password',
        name: 'Reset',
        component: () => import(/* webpackChunkName: "resetPassword" */  '../views/forgetPassword/index'),
      },
      {
        path: 'category',
        name: 'Classification',
        component: () => import(/* webpackChunkName: "classification" */  '../views/classification/index'),
      },
      {
        path: 'profile',
        name: 'Profile',
        component: () => import(/* webpackChunkName: "profile" */  '../views/profile/index'),
      },
    ],
  },
  {
    path: '/about',
    name: 'About',
    // route level code-splitting
    // this generates a separate chunk (about.[hash].js) for this route
    // which is lazy-loaded when the route is visited.
    // component: () => import(/* webpackChunkName: "about" */ '../views/About.vue'),
  },
]

const router = createRouter({
  history: createWebHashHistory(),
  routes,
})

router.beforeEach(async (to, from, next) => {
  if (!getToken()) {
    if (to.path === '/auth') {
      next()
      return
    }
    message.error('认证失败，请重新登入')
    next('/auth')
    return
  }
  const reqArr = []
  if (!store.state.user.account) {
    reqArr.push(store.dispatch('getUser'))
  }

  if (!store.state.roleList.length) {
    reqArr.push(store.dispatch('getRoleList'))
  }
  await Promise.all(reqArr)
  if (!store.state.role.identity) {
    store.commit('setRole', getRoleById(store.state.user.roleId))
  }
  if (to.path === '/auth') {
    next('/dashboard')
    return
  }
  next()
})

export default router
